package com.wave.wavesystem.config;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2024-12-19 22:24
 * @description
 */
@Data
@Component
public class TemplateMart {

    private Template Controller;
    private Template Mapper;
    private Template MapperXml;
    private Template DTO;
    private Template Entity;
    private Template VO;
    private Template Service;
    private Template ServiceImpl;
    private Template Wrapper;
    private Template Api;
    private Template View;
    private Template ViewDetail;
    private Template ViewRemove;
    private Template ViewSave;
    private Template ViewSearch;
    private Template ViewTable;
    private Template ViewUpdate;
    private Template ViewActionBar;
    private List<String> ftlPath = List.of(
            "template/backend/controller",
            "template/backend/mapper",
            "template/backend/pojo/dto",
            "template/backend/pojo/entity",
            "template/backend/pojo/vo",
            "template/backend/service",
            "template/backend/service/impl",
            "template/backend/wrapper",
            "template/frontend/api",
            "template/frontend/view",
            "template/frontend/view/components");

    @PostConstruct
    public void martInit() {
        for (String path : ftlPath) {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_33);
            cfg.setClassForTemplateLoading(this.getClass(), "/" + path);
            try {
                if (path.contains("controller")) {
                    Controller = cfg.getTemplate("Controller.ftl");
                    continue;
                }
                if (path.contains("mapper")) {
                    Mapper = cfg.getTemplate("Mapper.ftl");
                    MapperXml = cfg.getTemplate("Mapper.xml.ftl");
                    continue;
                }
                if (path.contains("dto")) {
                    DTO = cfg.getTemplate("QueryRequest.ftl");
                    continue;
                }
                if (path.contains("entity")) {
                    Entity = cfg.getTemplate("Entity.ftl");
                    continue;
                }
                if (path.contains("vo")) {
                    VO = cfg.getTemplate("VO.ftl");
                    continue;
                }
                if (path.contains("service") && !path.contains("service/impl")) {
                    Service = cfg.getTemplate("Service.ftl");
                    continue;
                }
                if (path.contains("service/impl")) {
                    ServiceImpl = cfg.getTemplate("ServiceImpl.ftl");
                    continue;
                }
                if (path.contains("wrapper")) {
                    Wrapper = cfg.getTemplate("Wrapper.ftl");
                    continue;
                }
                switch (path) {
                    case "template/frontend/api" -> Api = cfg.getTemplate("Api.ftl");
                    case "template/frontend/view" -> View = cfg.getTemplate("View.ftl");
                    case "template/frontend/view/components" -> {
                        ViewDetail = cfg.getTemplate("ViewDetail.ftl");
                        ViewRemove = cfg.getTemplate("ViewRemove.ftl");
                        ViewSave = cfg.getTemplate("ViewSave.ftl");
                        ViewSearch = cfg.getTemplate("ViewSearch.ftl");
                        ViewTable = cfg.getTemplate("ViewTable.ftl");
                        ViewUpdate = cfg.getTemplate("ViewUpdate.ftl");
                        ViewActionBar = cfg.getTemplate("ViewActionBar.ftl");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
